﻿package com.saint.elasticity.tests 
{
	import com.saint.elasticity.FixedAnchorPoint;
	import com.saint.elasticity.ElasticObject;
	import com.saint.elasticity.IAnchorPoint;
	import com.saint.utils.Logger;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;
	/**
	 * ...
	 * @author kembera
	 */
	public class ElasticityTest
	{
		private var springStrength:Number = 0.1;
		private var _buttons:Array = new Array();
		private var resetPositions:Array = new Array();
		private var _tileW:Number = 200;
		private var _tileH:Number = 200;
		private var scope:Sprite;
		
		public function ElasticityTest(scope:Sprite) 
		{
			Logger.info("ElasticityTest.ElasticityTest");
			
			this.scope = scope;
							
			
			/* create corner anchor points 
			var anchor0:FixedAnchorPoint = new FixedAnchorPoint(100, 100, true, springStrength);
			var anchor1:FixedAnchorPoint = new FixedAnchorPoint(300, 100, true, springStrength);
			var anchor2:FixedAnchorPoint = new FixedAnchorPoint(300, 300, true, springStrength);
			var anchor3:FixedAnchorPoint = new FixedAnchorPoint(100, 300, true, springStrength);*/
						
			/* add anchor points to scope 
			scope.addChild(anchor0);
			scope.addChild(anchor1);
			scope.addChild(anchor2);
			scope.addChild(anchor3);	*/		
			
			
			/* create the elastic objects 
			var elasticObject1:ElasticObject = new ElasticObject();
			var elasticObject2:ElasticObject = new ElasticObject();			
			var elasticObject3:ElasticObject = new ElasticObject();
			var elasticObject4:ElasticObject = new ElasticObject();			
			var elasticObject5:ElasticObject = new ElasticObject();	*/
			
			 /* set visibility of springs 
			elasticObject1.setShowSpring(true);					
			elasticObject2.setShowSpring(true);					
			elasticObject4.setShowSpring(true);					
			elasticObject5.setShowSpring(true);	*/				
			
			/* add elastic objects to scope 
			scope.addChild(elasticObject5);
			scope.addChild(elasticObject4);
			scope.addChild(elasticObject3);
			scope.addChild(elasticObject2);
			scope.addChild(elasticObject1);*/
			
			
			/* set elasticity anchor points 
			elasticObject1.attachSpringFrom(anchor0);
			elasticObject1.attachSpringFrom(anchor1);
			
			elasticObject2.attachSpringFrom(anchor0);
			elasticObject2.attachSpringFrom(anchor3);
			
			elasticObject3.attachSpringFrom(elasticObject1);		
			elasticObject3.attachSpringFrom(elasticObject5);		
			elasticObject3.attachSpringFrom(elasticObject2);		
			elasticObject3.attachSpringFrom(elasticObject4);		
			
			elasticObject4.attachSpringFrom(anchor1);
			elasticObject4.attachSpringFrom(anchor2);
			
			elasticObject5.attachSpringFrom(anchor3);
			elasticObject5.attachSpringFrom(anchor2);*/
			
			/* initiat the elastic objects 
			elasticObject1.init();
			elasticObject2.init();
			elasticObject3.init();
			elasticObject4.init();
			elasticObject5.init();*/
			
			
			/* create the crid */
			createElasticGrid(2, 2);
		}
		
				
		/**
		 * lays out the buttons on the scope
		 * 
		 * 	0	-	1	-	2	-	3	-	4
		 * 
		 * 	5	-	6	-	7	-	9	-	10
		 * 
		 * 	1	-	12	-	13	-	14	-	15
		 * 
		 * 
		 */
		private function layoutButtons(_cols, _rows):void
		{
			Logger.info("StretchyImage.layoutButtons");
			
			var leftBut:IAnchorPoint;
			var rightBut:IAnchorPoint;
			var topBut:IAnchorPoint;
			var bottomBut:IAnchorPoint;
			var currentElasticObj:ElasticObject;
			var index:Number;
			var pnt:Point;
			
			for (var i:Number = 0; i <= _rows; i++)
			{
				for (var n:Number = 0; n <= _cols; n++)
				{
					index = n + (i * (_cols + 1));
					pnt = new Point(n * _tileW,i * _tileH)
					_buttons[index].x = pnt.x;  
					_buttons[index].y = pnt.y;  
					//_buttons[index].alpha = buttonAlpha; 
					resetPositions.push(pnt);
					scope.addChild(_buttons[index]);	
					
					/* set the top row */
					if (i == 0)
					{
						/* topLeftCorner */
						if (n == 0) { trace(n + ", " + i + " : top left") }
						else
						
						/* topRight */
						if (n == _cols) { trace(n + ", " + i + " : top right") }
						
						else						
						{
							/* top row */
							trace(n + ", " + i + " : top row elastic objects")				
							currentElasticObj = _buttons[index];
							leftBut = _buttons[index - 1];
							rightBut = _buttons[index + 1];							
							currentElasticObj.attachSpringFrom(leftBut);
							currentElasticObj.attachSpringFrom(rightBut);
							currentElasticObj.init();						
						};
					}
					else
					/* set the bottom row */
					if (i == _rows )
					{
						/* bottomLeftCorner */
						if (n == 0) { trace(n + ", " + i + " : bottom left") }
						
						else
						
						/* bottmoRight */
						if (n == _cols) { trace(n + ", " + i + " : bottom right") }
						
						else						
						{							
							trace(n + ", " + i + " : bottom row");				
							currentElasticObj = _buttons[index];
							leftBut = _buttons[index - 1];
							rightBut = _buttons[index + 1];							
							currentElasticObj.attachSpringFrom(leftBut);
							currentElasticObj.attachSpringFrom(rightBut);
							currentElasticObj.init();	
						};
					}
					else
					/* set the left sides */
					if (n == 0)
					{
						trace(n + ", " + i + " : left side");
						currentElasticObj = _buttons[index];
						topBut = _buttons[index - (_cols+1)];
						bottomBut = _buttons[index + (_cols+1)];							
						currentElasticObj.attachSpringFrom(topBut);
						currentElasticObj.attachSpringFrom(bottomBut);
						currentElasticObj.init();	
						
					}
					else
					/* set the right sides */
					if (n == _cols )
					{
						trace(n + ", " + i + " : right side") ;
						currentElasticObj = _buttons[index];
						topBut = _buttons[index - (_cols+1)];
						bottomBut = _buttons[index + (_cols+1)];							
						currentElasticObj.attachSpringFrom(topBut);
						currentElasticObj.attachSpringFrom(bottomBut);
						currentElasticObj.init();	
					}
					/* set the interior ponts */
					else
					{
						trace(n + ", " + i + " : interior") ;			
						currentElasticObj = _buttons[index];
						topBut = _buttons[index - (_cols+1)];
						bottomBut = _buttons[index + (_cols+1)];
						leftBut = _buttons[index - 1];
						rightBut = _buttons[index + 1];	
						
						currentElasticObj.attachSpringFrom(leftBut);
						currentElasticObj.attachSpringFrom(rightBut);							
						currentElasticObj.attachSpringFrom(topBut);
						currentElasticObj.attachSpringFrom(bottomBut);
						currentElasticObj.init();				
					}
				}
			}
			
		}
		
		
		
		
		
		/**
		 * 
		create anchor points for the corners and elastic points for the rest of the gubbins
		
		 * @param	_rows
		 * @param	_cols
		 */
		private function createElasticGrid(_rows:uint, _cols:uint):void
		{
	
			Logger.info("ElasticityTest.createElasticGrid");
			
			var dragBtn:IAnchorPoint;
			for (var i:Number = 0; i < (_cols + 1) * (_rows + 1); i++)
			{			
				/* get the left side outer buttons == 0*/
				if (i % (_cols + 1) == 0)
				{
					if (i == 0 )
					{
						/* top left corner */
						dragBtn = new FixedAnchorPoint();
					}else if ( i == (_cols + 1) * _rows) {
						/* bottom left corner */
						dragBtn = new FixedAnchorPoint();						
					}else {
						/* left side */
						dragBtn = new ElasticObject();
					}
				}else 			
				/* get the right side buttons  == _cols*/
				if ((i % (_cols + 1) == _cols))
				{					
					if (i == _cols  )
					{					
						/* top right corner */
						dragBtn = new FixedAnchorPoint();
					}else if (i == _cols + (_cols + 1) * _rows) {		
						/* bottom left corner */
						dragBtn = new FixedAnchorPoint();
					}else{
						/* right side */	
						dragBtn = new ElasticObject();
					}
				}
				else {
					/* middle bits */
					dragBtn = new ElasticObject();
				}
							
			
				// make new IAnchorpiont				
				_buttons.push(dragBtn);
			}
			
			layoutButtons(_cols, _rows);
		
		}
		
		
	}

}